home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / DOSREF33.ZIP / CHAPTER.009 < prev    next >
Text File  |  1994-01-20  |  51KB  |  976 lines

  1.  
  2.        **  Programmer's Technical Reference for MSDOS and the IBM PC **
  3.                 USA copyright TXG 392-616  ALL RIGHTS RESERVED
  4.      ──────────────────────────┤ DOSREF (tm) ├───────────────────────────
  5.                      ISBN 1-878830-02-3 (disk-based text)
  6.                     Copyright (c) 1987, 1994 Dave Williams
  7.                         ┌─────────────────────────────┐
  8.                         │ Shareware Version, 01/20/94 │
  9.                         │  Please Register Your Copy  │
  10.                         └─────────────────────────────┘
  11.  
  12.                             C H A P T E R   N I N E
  13.  
  14.  
  15.                           INSTALLABLE DEVICE DRIVERS
  16.  
  17.  
  18.                                 C O N T E N T S
  19.  
  20. Device Driver Format .................................................... 9**
  21. Types of Devices ........................................................ 9**
  22.         Character Devices ............................................... 9**
  23.         Block Devices ................................................... 9**
  24. Device Header ........................................................... 9**
  25.         Pointer to Next Device Header Field ............................. 9**
  26.         Attribute Field ................................................. 9**
  27.                 Bits 0 and 1 ............................................ 9**
  28.                 Bit 2 ................................................... 9**
  29.                 Bit 3 ................................................... 9**
  30.                 Bit 13 .................................................. 9**
  31.                 Bit 14 .................................................. 9**
  32.                 Bit 15 .................................................. 9**
  33.         Pointer to Strategy and Interrupt Routines ...................... 9**
  34.         Name/Unit Field ................................................. 9**
  35. Creating a Device Driver ................................................ 9**
  36. Installing Device Drivers ............................................... 9**
  37.         Installing Character Devices .................................... 9**
  38.         Installing Block Devices ........................................ 9**
  39. Request Header .......................................................... 9**
  40.         Unit Code Field ................................................. 9**
  41.         Command Code Field .............................................. 9**
  42.         Status Field .................................................... 9**
  43. Device Driver Functions ................................................. 9**
  44.         INIT ............................................................ 9**
  45.         MEDIA CHECK ..................................................... 9**
  46.         Media Descriptor Byte ........................................... 9**
  47.         BUILD BPB (BIOS Parameter Block) ................................ 9**
  48.         INPUT OR OUTPUT ................................................. 9**
  49.         NONDESTRUCTIVE INPUT NO WAIT .................................... 9**
  50.         STATUS .......................................................... 9**
  51.         FLUSH ........................................................... 9**
  52.         OPEN or CLOSE (DOS 3.0+) ........................................ 9**
  53.         REMOVEABLE MEDIA (DOS 3.0+) ..................................... 9**
  54. The CLOCK$ Device ....................................................... 9**
  55.  
  56.  
  57.  
  58.  
  59. DEVICE DRIVER FORMAT├──────────────────────────────────────────────────────────
  60.  
  61.  A device driver is a handler for communication between the system software
  62. and hardware devices. The motherboard ROM and IBMBIO.COM or IO.SYS files
  63. contain the basic drivers for allowing DOS to talk to the console, disk drives,
  64. serial and parallel ports, clock, and other resources.
  65.  
  66.  DOS has five builtin drivers, STDIN, STDOUT, STERR, STDPRN, or STDAUX. An
  67. "installable" driver may be loaded in the CONFIG.SYS file, and either replace
  68. one of the builtin drivers or define a new resource, such as a mouse or
  69. expanded memory driver.
  70.  
  71.  The device driver is a COM (memory image) file that contains all of the code
  72. needed to control an add-in device. An EXE file should not be used since the
  73. EXE loader in some DOS 2.x versions is part of COMMAND.COM, which is not
  74. present when the device driver is being loaded by IBMBIO.COM or IO.SYS. EXE
  75. format drivers could be used in DOS 3.x+, but there is generally no reason to
  76. do so. The COM file must not load at the usual ORG 100h. Since the driver does
  77. not use the Program Segment Prefix, it is simply loaded without offset,
  78. therefore the driver file must have an origin of 0. Most references advise
  79. "ORG 0 or no ORG statement", however with the advent of many new assemblers on
  80. the market, some of which default to .COM files, specifically stating "ORG 0"
  81. may eliminate problems. Driver files should not have a declared stack segment.
  82.  
  83.  DOS can install the device driver anywhere in memory, so care must be taken
  84. in any FAR memory references. You should not expect that your driver will be
  85. loaded in the same place every time.
  86.  
  87.  
  88.  
  89.  
  90. TYPES OF DEVICES├──────────────────────────────────────────────────────────────
  91.  
  92.  There are two types of devices: Character devices and Block devices. Their
  93. attributes are as follows:
  94.  
  95.  Character devices are designed to do serial I/O in a byte-by-byte manner.
  96. These devices have names like CON, AUX, or PRN, and you can open channels
  97. (handles or FCBs) to do I/O much like a disk file. I/O may be in either cooked
  98. or raw mode. (see Chapter 7 for discussion of cooked and raw modes). Because
  99. character devices have only one name, they can only support one device.
  100.  
  101.  Block devices are normally implemented as disk drives. They can do random I/O
  102. in pieces called blocks, which are usually the physical sector size of the disk.
  103. These devices are not named as character devices are, and cannot be opened
  104. directly. Instead they are accessed by using drive letters such as A, B, C,
  105. etc. Block devices can have units within them. In this way, a single block
  106. driver can be responsible for one or more disk drives. For example, the first
  107. block device driver can be responsible for drives A, B, C, and D. This means it
  108. has four units defined and therefore takes up four drive letters. The position
  109. of the driver in the chain of all drives determines the way in which the drive
  110. letters correspond, i.e, if a second block device driver defines three units,
  111. then those units are E, F, and G.
  112.  
  113.  DOS 1.x allows 16 block devices. DOS 2.x allows 63, and DOS 3.x allows 26. It
  114. is recommended that drivers limit themselves to 26 devices for compatibility
  115. with DOS 3.x and 4.x. When DOS 2.x passes the Z: drivespec, the drivespecs get
  116. a little wierd, such as ^, [, or #. DOS 3.x+ will return an error message.
  117.  
  118.  
  119.  
  120.  
  121. CREATING A DEVICE DRIVER├──────────────────────────────────────────────────────
  122.  
  123.  To create a device driver that DOS can install, you must do the following:
  124.  
  125. 1) Create a memory image (COM) file with a device header at the start of the
  126.    file.
  127. 2) Originate the code (including the device header) at 0, instead of 100h.
  128. 3) Set the next device header field. Refer to "Pointer to Next Device Header
  129.    Attribute Field" for more information.
  130. 4) Set the attribute field of the device header. Refer to "Attribute Field" for
  131.    more information.
  132. 5) Set the entry points for the interrupt and strategy routines.
  133. 6) Fill in the name/unit field with the name of the character device or the
  134.    unit number of the block device.
  135.  
  136.  DOS always processes installable character device drivers before handling the
  137. default devices. So to install a new CON device, simply name the device CON.
  138. Be sure to set the standard input device and standard output device bits in
  139. the attribute field of a new CON device. The scan of the device list stops on
  140. the first match so the installable device driver takes precedence. For
  141. instance, installing ANSI.SYS replaces the builtin CON driver.
  142.  
  143.  DOS doesn't care about the position of installed character devices versus
  144. block devices.
  145.  
  146.  
  147.  
  148.  
  149. STRUCTURE OF A DEVICE DRIVER├──────────────────────────────────────────────────
  150.  
  151.  A device driver consists of three major parts:
  152.         a device header
  153.         a strategy routine
  154.         an interrupt routine
  155.  
  156.  
  157. DEVICE HEADER
  158.  
  159.  The driver has a special header to identify it as a device and to define the
  160. strategy and interrupt entry points and its various attributes. This header is
  161. located at the beginning of the file. It contains a pointer to the next driver
  162. in the chain, the attributes of the device, offsets into the strategy and
  163. interrupt routines, and the device ID.
  164.  
  165.  This is the format of the device header:
  166.  
  167. ┌──────────────────────────────────────────────────────────────────────────────┐
  168. │                         D E V I C E    H E A D E R                           │
  169. ├───────┬────────┬─────────────────────────────────────────────────────────────┤
  170. │Offset │ Length │                       Description                           │
  171. ├───────┼────────┼─────────────────────────────────────────────────────────────┤
  172. │  00h  │  word  │  Pointer to next device header field, offset value          │
  173. ├───────┼────────┼─────────────────────────────────────────────────────────────┤
  174. │  02h  │  word  │  Pointer to next device header field, segment value         │
  175. ├───────┼────────┼─────────────────────────────────────────────────────────────┤
  176. │  04h  │  word  │  Attribute                                                  │
  177. ├───────┼────────┼─────────────────────────────────────────────────────────────┤
  178. │  06h  │  word  │  Pointer to device strategy routine (offset only)           │
  179. ├───────┼────────┼─────────────────────────────────────────────────────────────┤
  180. │  08h  │  word  │  Pointer to device interrupt routine (offset only)          │
  181. ├───────┼────────┼─────────────────────────────────────────────────────────────┤
  182. │  0Ah  │8 bytes │  Name/Unit field                                            │
  183. └───────┴────────┴─────────────────────────────────────────────────────────────┘
  184.  
  185.  
  186. POINTER TO NEXT DEVICE HEADER FIELD
  187.  
  188.  The device header field is a pointer to the device header of the next device
  189. driver. It is a doubleword field that is set by DOS at the time the device
  190. driver is loaded. The first word is the offset and the second word is the
  191. segment.
  192.  
  193.  If you are loading only one device driver, set the device header field to -1
  194. before loading the device. If you are loading more than one device driver, set
  195. the first word of the device driver header to the offset of the next device
  196. driver's header. Set the device driver header field of the last device driver
  197. to -1.
  198.  
  199.  
  200. ATTRIBUTE FIELD
  201.  
  202.  The attribute field is a word field used to identify the type of device this
  203. driver is responsible for. This field distinguishes between block and
  204. character devices and determines is selected devices are given special
  205. treatment. The attributes are:
  206.  
  207. ┌──────────────────────────────────────────────────────────────────────────────┐
  208. │                        A T T R I B U T E   F I E L D                         │
  209. ├──────────┬───────┬───────────────────────────────────────────────────────────┤
  210. │   word   │ attr. │                                                           │
  211. ├──────────┼───────┤                     description                           │
  212. │   bits   │  set  │                                                           │
  213. ├──────────┼───────┼───────────────────────────────────────────────────────────┤
  214. │     0    │   0   │   not current standard input device                       │
  215. │          │   1   │   current standard input device                           │
  216. ├──────────┼───────┼───────────────────────────────────────────────────────────┤
  217. │     1    │   0   │   not current standard output device                      │
  218. │          │   1   │   current standard output device                          │
  219. ├──────────┼───────┼───────────────────────────────────────────────────────────┤
  220. │     2    │   0   │   not current NUL device                                  │
  221. │          │   1   │   current NUL device                                      │
  222. ├──────────┼───────┼───────────────────────────────────────────────────────────┤
  223. │     3    │   0   │   not current CLOCK device                                │
  224. │          │   1   │   current CLOCK device                                    │
  225. ├──────────┼───────┼───────────────────────────────────────────────────────────┤
  226. │     4    │   0   │   standard CON I/O routines should be used                │
  227. │          │   1   │   fast screen I/O (int 29h) should be used                │
  228. ├──────────┼───────┴───────────────────────────────────────────────────────────┤
  229. │  5 - 10  │         "reserved for DOS" - unknown - should be set to 0         │
  230. ├──────────┼───────┬───────────────────────────────────────────────────────────┤
  231. │    11    │   0   │   doesn't support removeable media  (default for DOS 2.x) │
  232. │          │   1   │   supports removeable media         (DOS 3.0+ only)       │
  233. ├──────────┼───────┴───────────────────────────────────────────────────────────┤
  234. │    12    │         "reserved for DOS" - unknown - should be set to 0         │
  235. ├──────────┼───────┬───────────────────────────────────────────────────────────┤
  236. │    13    │   0   │   IBM format       (block devices)                        │
  237. │          │   1   │   non-IBM format   (block devices)                        │
  238. │          │   1   │   output till busy (character devices)                    │
  239. ├──────────┼───────┼───────────────────────────────────────────────────────────┤
  240. │    14    │   0   │   doesn't support IOCTL                                   │
  241. │          │   1   │   supports IOCTL                                          │
  242. ├──────────┼───────┼───────────────────────────────────────────────────────────┤
  243. │    15    │   0   │   block device                                            │
  244. │          │   1   │   character device                                        │
  245. └──────────┴───────┴───────────────────────────────────────────────────────────┘
  246.  
  247. note 1) If a bit in the attribute word is defined only for one type of device,
  248.         a driver for the other type of device must set that bit to 0.
  249.      2) For DOS 2.0 bits 4-12 must be off.
  250.  
  251.  
  252. BIT 1   is the standard input and output bit. It is used for character devices
  253.         only. Use this bit to tell DOS if your character device driver is the
  254.         new standard input device or standard output device.
  255.  
  256. BIT 2   is the NUL attribute bit. It is used for character devices only. Use it
  257.         to tell DOS if your character device driver is a NUL device. Although
  258.         there is a NUL device attribute bit, you cannot reassign the NUL
  259.         device or replace it with your own routine. This attribute exists for
  260.         DOS so that DOS can tell if the NUL device is being used.
  261.  
  262. BIT 3   is the clock device bit. It is used for character devices only. Default
  263.         is 0. You can use it to tell DOS if your character device driver is the
  264.         new CLOCK device.
  265.  
  266. BIT 4   is the "fast video output" bit. The default is 0, which uses the BIOS
  267.         for writing to the screen. When set, this bit uses int 29h for much
  268.         faster screen updates.
  269.  
  270. BITS 5-10  reserved for DOS, unknown. Should be set to 0.
  271.  
  272. BIT 11  is the open/close removeable media bit. Use it to tell DOS if the
  273.         device driver can handle removeable media. This bit is valid for DOS
  274.         3.0+ only. This bit was reserved in DOS 2.x. Since DOS 2.x does not
  275.         look at this bit, its use is backward compatible.
  276.  
  277. BIT 12  reserved for DOS, unknown. Should be set to 0.
  278.  
  279. BIT 13  is the non-IBM format bit. When used for block devices it affects the
  280.         operation of the BUILD BPB (BIOS parameter block) device call. For
  281.         character devices it indicates that the devices implements the OUTPUT
  282.         UNTIL BUSY device call.
  283.  
  284. BIT 14  is the IOCTL bit. It is used for both character and block devices. Use
  285.         it to tell DOS whether the device driver can handle control strings
  286.         through the IOCTL function call 44h.
  287.          If a device driver cannot process control strings, it should set bit
  288.         14 to 0. This way DOS can return an error if an attempt is made through
  289.         the IOCTL function call to send or receive control strings to the
  290.         device. If a device can process control strings, it should set bit 14
  291.         to 1. This way, DOS makes calls to the IOCTL input and output device
  292.         function to send and receive IOCTL strings.
  293.          The IOCTL functions allow data to be sent to and from the device
  294.         without actually doing a normal read or write. In this way, the device
  295.         driver can use the data for its own use, (for example, setting a baud
  296.         rate or stop bits, changing form lengths, etc.) It is up to the device
  297.         to interpret the information that is passed to it, but the information
  298.         must not be treated as a normal I/O request.
  299.  
  300. BIT 15  is the device type bit. Use it to tell the system the that driver is a
  301.         block or character device.
  302.  
  303.  
  304. POINTER TO STRATEGY ROUTINE
  305.  
  306.  This field contains a pointer to "device strategy" function in the driver.
  307. This function is called whenever a request is made to the driver, and must
  308. store the location of the request header from DOS. This pointer is a word
  309. value, and so must be in the same segment as the device header.
  310.  
  311.  
  312. POINTER TO INTERRUPT ROUTINE
  313.  
  314.  This field contains a pointer to the function which activates driver routines
  315. to perform the command in the current request header. This is called by DOS
  316. after the call to the strategy function, and should reset to the request header
  317. address stored by "strategy", to allow for the possibility of interrupts
  318. between the two calls. This pointer is a word value, and so must be in the same
  319. segment as the device header.
  320.  
  321.  
  322. NAME/UNIT FIELD
  323.  
  324.  This is an 8-byte field that contains the name of a character device or the
  325. number of units in a block device. For the character names, the name is
  326. left-justified and the space is filled to 8 bytes. For block devices, the
  327. number of units can be placed in the first byte. This is optional because DOS
  328. fills in this location with the value returned by the driver's INIT code. The
  329. other 7 bytes of the block device ID are reserved and should not be used.
  330.  
  331.  
  332.  
  333. INSTALLING DEVICE DRIVERS├─────────────────────────────────────────────────────
  334.  
  335.  DOS installs new device drivers dynamically at boot time by reading and
  336. processing the DEVICE command in the CONFIG.SYS file. For example, if you have
  337. written a device driver called RAMDISK, to install it put this command in the
  338. CONFIG.SYS file:
  339.                    DEVICE=[drive][path] RAMDISK [parameters]
  340.  
  341.  DOS makes a FAR call to the device driver at its strategy entry point first,
  342. using the request header to pass information describing what DOS wants the
  343. device driver to do.
  344.  
  345.  This strategy routine does not perform the request but rather queues the
  346. request or saves a pointer to the request header. The second entry point is
  347. the interrupt routine and is called by DOS immediately after the strategy
  348. routine returns. The interrupt routine is called with no parameters. Its
  349. function is to perform the operation based on the queued request and set up
  350. any return infromation.
  351.  
  352.  DOS passes the pointer to the request header in ES:BX. This structure consists
  353. of a fixed length header (Request Header) followed by data pertinent to the
  354. operation to be performed.
  355.  
  356. NOTE: It is the responsibility of the device driver to preserve the machine
  357.       state. For example, save all registers on entry and restore them on exit.
  358.  
  359.  The stack used by DOS has enough room on it to save all the registers. If more
  360. stack space is needed, it is the device driver's responsibility to allocate and
  361. maintain another stack.
  362.  
  363.  All calls to execute device drivers are FAR calls. FAR returns should be
  364. executed to return to DOS.
  365.  
  366.  
  367.  
  368. INSTALLING CHARACTER DEVICES
  369.  
  370.   One of the functions defined for each device is INIT. This routine is called
  371. only once when the device is installed and never again. The INIT routine returns
  372. the following:
  373.  
  374. A) A location to the first free byte of memory after the device driver, like a
  375.    TSR that is stored in the terminating address field. This way, the
  376.    initialization code can be used once and then thrown away to save space.
  377. B) After setting the address field, a character device driver can set the status
  378.    word and return.
  379.  
  380.  
  381.  
  382. INSTALLING BLOCK DEVICES
  383.  
  384.  Block devices are installed in the same way as character devices. The
  385. difference is that block devices return additional information. Block devices
  386. must also return:
  387.  
  388. A) The number of units in the block device. This number determines the logical
  389.    names the devices will have. For example, if the current logical device
  390.    letter is F at the time of the install call, and the block device driver INIT
  391.    routine returns three logical units, the letters G, H, and I are assigned to
  392.    the units. The mapping is determined by the position of the driver in the
  393.    device list and the number of units in the device. The number of units
  394.    returned by INIT overrides the value in the name/unit field of the device
  395.    header.
  396.  
  397. B) A pointer to a BPB (BIOS Parameter Block) pointer array. This is a pointer
  398.    to an array of "N" word pointers there "N" is the number of units defined.
  399.    These word pointers point to BPBs. This way, if all of the units are the
  400.    same, the entire array can point to the same BPB to save space.
  401.     The BPB contains information pertinent to the devices such as the sector
  402.    size, number of sectors per allocation unit, and so forth. The sector size of
  403.    the BPB cannot be greater than the maximum allotted size set at DOS
  404.    initialization time. This array must be protected below the free pointer set
  405.     by the return.
  406.  
  407. C) The media descriptor byte. This byte is passed to devices so that they know
  408.    what parameters DOS is currently using for a particular drive unit.
  409.  
  410.  Block devices can take several approaches. They can be "dumb" or "smart". A
  411. dumb device would define a unit (and therefore a BPB) for each possible media
  412. drive combination. Unit 0=drive 0;single side, unit 1=drive 0;double side, etc.
  413. For this approach, the media descriptor bytes would mean nothing. A smart
  414. device would allow multiple media per unit. In this case, the BPB table
  415. returned at INIT must define space large enough to acommodate the largest
  416. possible medias supported (sector size in BPB must be as large as maximum
  417. sector size DOS is currently using). Smart drivers will use the media
  418. descriptor byte to pass information about what media is currently in a unit.
  419.  
  420.  
  421.  
  422. REQUEST HEADER├────────────────────────────────────────────────────────────────
  423.  
  424.  The request header passes the information describing what DOS wants the
  425. device driver to do.
  426.  When a valid device driver command code or function is called by your
  427. application program, DOS develops a data structure called the "Request Header"
  428. in ES:BX and passes it to the strategy entry point. This structure consists of
  429. a 13-byte defined header which may be followed by other data bytes depending on
  430. the function requested.
  431.  It is the device driver's responsibility to preserve the machine state, for
  432. example, saving all registers including flags on entry and restoring them on
  433. exit. There is enough room on the stack when strategy or interrupt is called
  434. to do about 20 pushes. If more stack is needed, the driver should set aside
  435. its own stack space.
  436.  The fixed ("static") part of the request header is as follows:
  437.  
  438. ┌────────────────────────────────────────────────────────────────────────────┐
  439. │                        R E Q U E S T    H E A D E R                        │
  440. ├───────┬───────┬────────────────────────────────────────────────────────────┤
  441. │Offset │Length │                      F i e l d                             │
  442. ├───────┼───────┼────────────────────────────────────────────────────────────┤
  443. │  00h  │  byte │ Length in bytes of the request header plus any data at end │
  444. ├───────┼───────┼────────────────────────────────────────────────────────────┤
  445. │  01h  │  byte │ Unit code. Determines subunit to use in block devices      │
  446. │       │       │ (minor device)  Has no meaning for character devices       │
  447. ├───────┼───────┼────────────────────────────────────────────────────────────┤
  448. │  02h  │  byte │ Command code                                               │
  449. ├───────┼───────┼────────────────────────────────────────────────────────────┤
  450. │  03h  │  word │ Status                                                     │
  451. ├───────┼───────┼────────────────────────────────────────────────────────────┤
  452. │  05h  │8 bytes│ Reserved for DOS                                           │
  453. ├───────┼───────┼────────────────────────────────────────────────────────────┤
  454. │  0Ch  │varies │ Data appropriate for the operation                         │
  455. └───────┴───────┴────────────────────────────────────────────────────────────┘
  456.  
  457. REQUEST HEADER LENGTH FIELD
  458.  
  459.  The length in bytes of the total request header (0-255) plus any data at the
  460. end of the header.
  461.  
  462.  
  463. UNIT CODE FIELD
  464.  
  465.  The unit code field identifies which unit in a block device driver the request
  466. is for. For example, if a block device driver has three units defined, then the
  467. possible values of the unit code field would be 0, 1, and 2. This field is not
  468. valid for character devices.
  469.  
  470.  
  471. COMMAND CODE FIELD
  472.  
  473.  The command code invokes a specific device driver function. Functions 0
  474. through 12 are supported in all device drivers. Functions 13-15 are available
  475. only in DOS 3.0 or higher. Some functions are relevant for either character or
  476. block devices but not both; nonetheless all functions must have an executable
  477. routine present even if it does nothing but set the done flag in the return
  478. status word in the request header.
  479.  
  480.  The command code field in the request header can have the following values:
  481. ┌──────┬──────────────────┬────────────────────────────────────────────────────┐
  482. │ code │       name       │                     function                       │
  483. ├──────┼──────────────────┼────────────────────────────────────────────────────┤
  484. │   0  │ INIT             │ initialize driver for later use (used once only)   │
  485. │   1  │ MEDIA CHECK      │ block devices only, NOP for character devices      │
  486. │   2  │ BUILD BPB        │ block devices only, NOP for character devices      │
  487. │   3  │ IOCTL input      │ called only if device has IOCTL bit set            │
  488. │   4  │ INPUT            │ read data                                          │
  489. │   5  │ NONDESTRUCTIVE INPUT NO WAIT  character devices only                  │
  490. │   6  │ INPUT STATUS     │ character devices only                             │
  491. │   7  │ INPUT FLUSH      │ character devices only                             │
  492. │   8  │ OUTPUT           │ write data                                         │
  493. │   9  │ OUTPUT           │ write data with verify                             │
  494. │  10  │ OUTPUT STATUS    │ character devices only                             │
  495. │  11  │ OUTPUT FLUSH     │ character devices only                             │
  496. │  12  │ IOCTL OUTPUT     │ called only if device has IOCTL bit is set         │
  497. │  13  │ DEVICE OPEN      │ called only if OPEN/CLOSE/RM bit is set            │
  498. │  14  │ DEVICE CLOSE     │ called only if OPEN/CLOSE/RM bit is set            │
  499. │  15  │ REMOVEABLE MEDIA │ only if OPEN/CLOSE/RM bit set & device is block    │
  500. │  16  │ OUTPUT UNTIL BUSY│ only called if bit 13 is set & device is character │
  501. └──────┴──────────────────┴────────────────────────────────────────────────────┘
  502.  
  503.  The individual command codes are described later in this chapter.
  504.  
  505.  
  506.  
  507. STATUS FIELD
  508.  
  509.  The status word field is zero on entry and is set by the driver interrupt
  510. routine on return.
  511.  
  512. The status field in the request header contains:
  513.  
  514. ┌─────────────────────────────────────────────────────────────────────────────┐
  515. │            D E V I C E    D R I V E R    S T A T U S    F I E L D           │
  516. ├───────┬───┬─────────────────────────────────────────────────────────────────┤
  517. │ size  │bit│                        definition                               │
  518. ├───────┼───┼─────────────────────────────────────────────────────────────────┤
  519. │ byte  │ 0 │                                                                 │
  520. │       │ 1 │                                                                 │
  521. │       │ 2 │                                                                 │
  522. │       │ 3 │  Error message return code                                      │
  523. │       │ 4 │  (with bit 15=1)                                                │
  524. │       │ 5 │                                                                 │
  525. │       │ 6 │                                                                 │
  526. │       │ 7 │                                                                 │
  527. ├───────┼───┼─────────────────────────────────────────────────────────────────┤
  528. │ byte  │ 8 │  DONE                                                           │
  529. │       ├───┼─────────────────────────────────────────────────────────────────┤
  530. │       │ 9 │  BUSY                                                           │
  531. │       ├───┼─────────────────────────────────────────────────────────────────┤
  532. │       │ A │  Reserved by DOS, unknown                                       │
  533. │       │ B │                                                                 │
  534. │       │ C │                                                                 │
  535. │       │ D │                                                                 │
  536. │       │ E │                                                                 │
  537. │       ├───┼─────────────────────────────────────────────────────────────────┤
  538. │       │ F │  Error                                                          │
  539. └───────┴───┴─────────────────────────────────────────────────────────────────┘
  540.  
  541.  
  542.  The low 8 bits of the status word define an error message if bit 15 is set.
  543. These errors are:
  544.  
  545.         00h  write protect violation   01h  unknown unit
  546.         02h  device not ready          03h  unknown command
  547.         04h  CRC error                 05h  bad drive request structure length
  548.         06h  seek error                07h  unknown media
  549.         08h  sector not found          09h  printer out of paper
  550.         0Ah  write fault               0Bh  read fault
  551.         0Ch  general failure           0Dh  reserved
  552.         0Eh  reserved                  0Fh  invalid disk change
  553.  
  554. BIT 8   is the done bit. If it is set, it means the operation is complete. The
  555.         driver sets the bit to 1 when it exits.
  556.  
  557. BIT 9   is the busy bit. It is only set by status calls and the removable media
  558.         call.
  559.  
  560. BITS 10-14 are reserved.
  561.  
  562. BIT 15  is the error bit. If this bit is set, the low 8 bits of the status word
  563.         (7-0) indicate the error code.
  564.  
  565.  
  566. RESERVED FOR DOS
  567.  Official sources label this area as "reserved for DOS". Another source
  568. indicates that this consists of two double-word (4-byte) pointers to be used
  569. to maintain a linked list of request headers for this device and a list of all
  570. current device requests being processed by DOS. This was apparently to be used
  571. for the undelivered multitasking version of DOS.
  572.  
  573.  
  574.  
  575. DEVICE DRIVER FUNCTIONS├───────────────────────────────────────────────────────
  576.  
  577.  All strategy routines are called with ES:BX pointing to the request header.
  578. The interrupt routines get the pointers to the request header from the queue
  579. the strategy routines stores them in. The command code in the request header
  580. tells the driver which function to perform.
  581.  
  582. NOTE: All DWORD pointers are stored offset first, then segment.
  583.  
  584.                          ############################
  585.  
  586. INIT
  587. Command code = 0        (all devices)
  588.         Performs all initialization required at DOS boot time to install the
  589.         driver and set local driver variables. This function is called only
  590.         once, when the driver is loaded.
  591.  
  592.         ES:BX   pointer to 26-byte request header and data structure
  593.  Format of structure:
  594.        offset     length          field
  595.          00h    13 bytes   request header
  596.          0Dh       byte    number of units (not set by character devices)
  597.          11h       dword   ending address of the driver's resident code
  598.          15h       dword   pointer to BPB array (not set by character devices)
  599.                            /pointer to remainder of arguments
  600.          19h       byte    drive number (DOS 3.0+ only)
  601.  
  602.  
  603.  When INIT is called, the driver must do the following:
  604.  
  605.         A) set the number of units (block devices only)
  606.         B) set up the pointer to the BPB array (block devices only)
  607.         C) perform any initialization code (to modems, printers, etc)
  608.         D) set the ending address of the resident program code
  609.         E) set the status word in the request header
  610.  
  611.  To obtain information obtained from CONFIG.SYS to a device driver at INIT
  612. time, the BPB pointer field points to a buffer containing the information
  613. passed from CONFIG.SYS following the =. The buffer that DOS passes to the
  614. driver at INIT after the file specification contains an ASCII string for the
  615. file OPEN. The ASCII string (ending in 0h) is terminated by a carriage return
  616. (0Dh) and linefeed (0Ah). If there is no parameter information after the file
  617. specification, the file specification is immediately followed by a linefeed
  618. (0Ah).
  619.  
  620.  NOTE: This information is read-only and only system calls 01h-0Ch and 30h can
  621.        be issued by the INIT code of the driver.
  622.  
  623.  The last byte parameter contains the drive letter for the first unit of a
  624. block driver. For example, 0=A, 1=B etc.
  625.  
  626.  If an INIT routine determines that it cannot set up the device and wants to
  627. abort without using any memory, follow this procedure:
  628.  
  629.         A) set the number of units to 0
  630.         B) set the ending offset address at 0
  631.         C) set the ending offsret segment address to the code segment (CS)
  632.  
  633. NOTE: If there are multiple device drivers in a single memory image file, the
  634.       ending address returned by the last INIT called is the one DOS uses. It is
  635.       recommended that all device drivers in a single memory image file return
  636.       the same ending address.
  637.  
  638.                          ############################
  639.  
  640. MEDIA CHECK
  641. command code = 1        (block devices only)
  642.         Checks to see if disk had been changed since last access.
  643.  
  644.         ES:BX   pointer to 19-byte request header and data structure
  645.  Format of structure:
  646.         offset  length          field
  647.         00h     13 bytes   request header
  648.         0Dh        byte    media descriptor from BPB
  649.         0Eh        byte    returned
  650.         0Fh       dword    returns a pointer to the previous volume ID (if bit
  651.                            11=1 and disk change is returned) (DOS 3.0+)
  652.  
  653.  When the command code field is 1, DOS calls MEDIA CHECK for a drive unit and
  654. passes its current media descriptor byte. See "Media Descriptor Byte" later in
  655. this chapter for more information about the byte. MEDIA CHECK returns one of
  656. the following:
  657.  
  658.         A) media not changed             C) not sure
  659.         B) media changed                 D) error code
  660.  
  661. The driver must perform the following:
  662.         A) set the status word in the request header
  663.         B) set the return byte
  664.                 00h   don't know if media has been changed
  665.                 01h   media has not been changed
  666.                 -1    media has been changed
  667.  
  668.  DOS 3.0+: If the driver has set the removable media bit 11 of the device header
  669. attribute word to 1 and the driver returns -1 (media changed), the driver must
  670. set the DWORD pointer to the previous volume identification field. If DOS
  671. determines that the media changed is an error, DOS generates an error 0Fh
  672. (invalid disk change) on behalf of the device. If the driver does not implement
  673. volume identification support, but has bit 11 set to 1, the driver should set a
  674. pointer to the string "NO NAME",0.
  675.  
  676.  
  677. MEDIA DESCRIPTOR
  678.  Currently the media descriptor byte has been defined for a few media types.
  679. This byte should be identical to the media byte if the device has the non-IBM
  680. format bit off. These predetermined values are:
  681.  
  682. media descriptor byte =>    1  1  1  1  1  0  0  0
  683.  (numeric order)            7  6  5  4  3  2  1  0
  684.  
  685.        BIT                MEANING
  686.  
  687.         0       0       not double sided
  688.                 1       double sided
  689.         1       0       not 8 sector
  690.                 1       8 sector
  691.         2       0       nonremoveable
  692.                 1       removeable
  693.        3-7      must be set to 1
  694.  
  695.                          ############################
  696.  
  697. BUILD BPB (BIOS Parameter Block)
  698. command code = 2        (block devices only)
  699.  
  700.         ES:BX   pointer to 22-byte request header and data structure
  701.  Format of structure:
  702.         offset   length          field
  703.         00h     13 bytes   request header
  704.         0Dh        byte    media descriptor from DOS
  705.         0Eh       dword    transfer address (buffer address)
  706.         12h       dword    pointer to BPB table
  707.  
  708. DOS calls BUILD BPB under the following two conditions:
  709.  
  710. A) If "media changed" is returned.
  711. B) If "not sure" is returned. If so, there are no used buffers. Used buffers
  712.    are buffers with changed data that have not yet been written to the disk.
  713.  
  714. The driver must do the following:
  715.  
  716. A) set the pointer to the BPB.
  717. B) set the status word in the request header.
  718.  
  719.  The driver must determine the correct media type currently in the unit to
  720. return the pointer to the BPB table. The way the buffer is used (pointer
  721. passed by DOS) is determined by the non-IBM format bit in the attribute field
  722. of the device header. If bit 13=0 (device is IBM compatible), the buffer
  723. contains the first sector of the FAT (most importantly the FAT ID byte). The
  724. driver must not alter this buffer in this case. If bit 13=1 the buffer is a
  725. one sector scratch area which can be used for anything.
  726.  
  727.  For drivers that support volume identification and disk change, the call
  728. should cause a new volume identification to be read off the disk. This call
  729. indicates that the disk has been legally changed.
  730.  
  731.  If the device is IBM compatible, it must be true that the first sector of the
  732. first FAT is located at the same sector for all possible media. This is
  733. because the FAT sector is read before the media is actually determined.
  734.  
  735.  The information relating to the BPB for a particular media is kept in the boot
  736. sector for the media. In particular, the format of the boot sector is:
  737.  
  738. ┌──────────────────────────────────────────────────────────────────────────────┐
  739. │ For DOS 2.x, 3 byte near jump (0E9h) For DOS 3.x+, 2 byte near jump (0EBh)   │
  740. │ followed by a NOP (90h)                                                      │
  741. ├──────────┬───────────────────────────────────────────────────────────────────┤
  742. │ 8 bytes  │  OEM name and version                                             │
  743. ├──────────┼─────┬─────────────────────────────────────────────────────────────┤
  744. │   BYTE   │     │  sectors per allocation unit (must be a power of 2)         │
  745. ├──────────┤     ├─────────────────────────────────────────────────────────────┤
  746. │   WORD   │  B  │  reserved sectors (strarting at logical sector 0)           │
  747. ├──────────┤     ├─────────────────────────────────────────────────────────────┤
  748. │   BYTE   │     │  number of FATs                                             │
  749. ├──────────┤     ├─────────────────────────────────────────────────────────────┤
  750. │   WORD   │  P  │  max number of root directory entries                       │
  751. ├──────────┤     ├─────────────────────────────────────────────────────────────┤
  752. │   WORD   │     │  number of sectors in logical image (total number of        │
  753. │          │     │  sectors in media, including boot sector directories, etc.) │
  754. ├──────────┤  B  ├─────────────────────────────────────────────────────────────┤
  755. │   BYTE   │     │  media descriptor                                           │
  756. ├──────────┤     ├─────────────────────────────────────────────────────────────┤
  757. │   WORD   │     │  number of sectors occupied by a single FAT                 │
  758. ├──────────┼─────┴─────────────────────────────────────────────────────────────┤
  759. │   WORD   │  sectors per track                                                │
  760. ├──────────┼───────────────────────────────────────────────────────────────────┤
  761. │   WORD   │  number of heads                                                  │
  762. ├──────────┼───────────────────────────────────────────────────────────────────┤
  763. │   WORD   │  number of hidden sectors                                         │
  764. └──────────┴───────────────────────────────────────────────────────────────────┘
  765.  
  766.  The three words at the end return information about the media. The number of
  767. heads is useful for supporting different multihead drives that have the same
  768. storage capacity but a different number of surfaces. The number of hidden
  769. sectors is useful for drive partitioning schemes.
  770.  
  771.                          ############################
  772.  
  773. INPUT / OUTPUT  (IOCTL)
  774. command code = 3   IOCTL Read
  775.                4   Read              (block or character devices)
  776.                8   Write             (block or character devices)
  777.                9   Write With Verify
  778.               12   IOCTL Write
  779.               16   Output Until Busy (character devices only)
  780.  
  781.         ES:BX   pointer to 24-byte request header and data structure
  782.  
  783.   Format of structure:
  784.         offset    length          field
  785.         00h     13 bytes     request header
  786.         0Dh        byte      media descriptor byte from BPB
  787.         0Eh       dword      transfer address (buffer address)
  788.         12h        word      byte/sector count
  789.         14h        word      starting sector number (block devices)
  790.                              [no meaning on character devices]
  791.         16h       dword      (DOS 3.0+) pointer to the volume ID if error code
  792.                              0Fh is returned
  793.  
  794. The driver must perform the following:
  795.         A) set the status word in the request header
  796.         B) perform the requested function
  797.         C) set the actual number of sectors or bytes tranferred
  798.  
  799.  No error checking is performed on an IOCTL I/O call. However, the driver must
  800. set the return sector or byte count to the actual number of bytes transferred.
  801.  
  802.  Under certain circumstances a block device driver may be asked to do a write
  803. operation of 64k bytes that seems to be a "wrap around" of the transfer address
  804. in the BIOS I/O packet. This arises due to an optimization added to write code
  805. in DOS. It will only happen in writes that are within a sector size of 64k on
  806. files that are being extended past the current end of file. It is allowable for
  807. the device driver to ignore the balance of the write that wraps around, if it
  808. so chooses. For example, a write of 10000h bytes worth of sectors with a
  809. transfer address of XXX:1 ignores the last two bytes. A user program can never
  810. request an I/O of more than 0FFFFh bytes and cannot wrap around (even to 0) in
  811. the transfer segment, so in that case the last two bytes can be ignored.
  812.  
  813.  A program that uses DOS function calls can never request an input or output
  814. function of more than 0FFFFh bytes, therefore, a wrap around in the transfer
  815. (buffer) segment can never occur. It is for this reason you can ignore bytes
  816. that would have wrapped around in the tranfer segment.
  817.  
  818.  If the driver returns an error code of 0Fh (invalid disk change) it must put
  819. a DWORD pointer to an ASCIIZ string which is the correct volume ID to ask the
  820. user to reinsert the disk.
  821.  
  822. DOS 3.0+:
  823.  The reference count of open files on the field (maintained by the OPEN and
  824. CLOSE calls) allows the driver to determine when to return error 0Fh. If there
  825. are no open files (reference count=0) and the disk has been changed, the I/O
  826. is all right, and error 0Fh is not returned. If there are open files
  827. (reference count > 0) and the disk has been changed, an error 0Fh condition
  828. may exist.
  829.  
  830.                          ############################
  831.  
  832. NONDESTRUCTIVE INPUT NO WAIT
  833. command code = 5        (character devices only)
  834.         Reads a character from input stream but does not remove it from the
  835.         buffer
  836.  
  837.         ES:BX   pointer to 14-byte request header and data structure
  838.  Format of structure:
  839.         offset    length          field
  840.         00h     13 bytes   request header
  841.         0Dh        byte    read from device
  842.  
  843. The driver must do the following:
  844.         A) return a byte from the device
  845.         B) set the status word in the request header.
  846.  
  847.  If the character device returns busy bit=0 (characters in the buffer), then
  848. the next character that would be read is returned. This character is not removed
  849. form the buffer (hence the term nondestructive input). This call allows DOS to
  850. look ahead one character.
  851.  
  852.                          ############################
  853.  
  854. STATUS
  855. command codes =  6   Input Status      (character devices only)
  856.                 10   Output Status     (character devices only)
  857.         Check for characters waiting in input buffer
  858.  
  859.         ES:BX   pointer to 13-byte request header
  860.  
  861. This driver must perform the following:
  862.         A) perform the requested function
  863.         B) set the busy bit
  864.         C) set the status word in the request header.
  865.  
  866. The busy bit is set as follows:
  867.  
  868.  For input on unbuffered character devices: if the busy bit (bit 9) is 1 on
  869. return, a write request would wait for completion of a current request. If the
  870. busy bit is 0, there is no current request. Therefore, a write request would
  871. start immediately.
  872.  
  873.  For input on buffered character devices: if the busy bit is 1 on return, a
  874. read request does to the physical device. If the busy bit is 0, there are
  875. characters in the device buffer and a read returns quickly. It also indicates
  876. that a user has typed something. DOS assumes all character devices have a type-
  877. ahead input buffer. Devices that do not have this buffer should always return
  878. busy=0 so that DOS does not hang waiting for information to be put in a buffer
  879. that does not exist.
  880.  
  881.                          ############################
  882.  
  883. FLUSH INPUT BUFFERS
  884. command code = 7        (character devices only)
  885.         Forces all data in buffers to specified device.
  886.  
  887.         ES:BX   pointer to 13-byte request header
  888.  
  889.  This call tells the driver to flush (terminate) all pending requests that it
  890. has knowledge of. Its primary use is to flush the input queue on character
  891. devices.
  892.  
  893.  The driver must set the status word in the request header upon return.
  894.  
  895.                          ############################
  896.  
  897. FLUSH OUTPUT BUFFERS
  898. command code 11         (character devices only)
  899.         Forces all data in buffers to specified device.
  900.  
  901.         ES:BX   pointer to 13-byte request header
  902.  
  903.  
  904.  This call tells the driver to flush all output buffers and discards any
  905. pending requests. Its primary use is to flush the output queue on character
  906. devices.
  907.  
  908.  The driver must set the status word in the request header upon return.
  909.  
  910.                          ############################
  911.  
  912. OPEN or CLOSE  (DOS 3.0+)
  913. command code = 13   Open      (block or character devices)
  914.                14   Close     (block or character devices)
  915.  
  916.         ES:BX   pointer to 13-byte static request header
  917.  
  918.  These calls are designed to give the device information about the current file
  919. activity on the device if bit 11 of the attribute word is set. On block
  920. devices, these calls can be used to manage local buffering. The device can keep
  921. a reference count. Every OPEN causes the device to increment the reference
  922. count. Every CLOSE causes the device to decrement the reference count. When the
  923. reference count is 0, if means there are no open files in the device. Therefore,
  924. the device should flush buffers inside the device it has written to because now
  925. the user can change the media on a removeable media drive. If the media had been
  926. changed, it is advisable to reset the reference count to 0 without flushing the
  927. buffers. This can be thought of as "last close causes flush". These calls are
  928. more useful on character devices. The OPEN call can be used to send a device
  929. initialization string. On a printer, this could cause a string to be sent to set
  930. the font, page size, etc. so that the printer would always be in a known state
  931. in the I/O stream. Similarly, a CLOSE call can be used to send a post string
  932. (like a form feed) at the end of an I/O stream. Using IOCTL to set these pre and
  933. post strings provides a flexible mechanism of serial I/O device stream control.
  934.  
  935.  Since all processes have access to STDIN, STDOUT, STDERR, STDAUX, and STDPRN
  936. (handles 0, 1, 2, 3, and 4) the CON, AUX, and PRN devices are always open.
  937.  
  938.                          ############################
  939.  
  940. REMOVABLE MEDIA  (DOS 3.0+)
  941. command code = 15       (block devices only)
  942.         This call identifies the media type as removable or nonremovable.
  943.  
  944.         ES:BX   pointer to 13-byte static request header
  945.  
  946.  To use this call, set bit 11 (removable media) of the attribute field to 1.
  947. Block devices can only use this call through a subfunction of the IOCTL
  948. function call (int 21h fn44h).
  949.  This call is useful because it allows a utility to know whether it is dealing
  950. with a nonremovable media drive or with a removable media drive. For example,
  951. the FORMAT utility needs to know whether a drive is removable or nonremovable
  952. because it prints different versions of some prompts.
  953.  
  954. note    No error checking is performed. It is assumed that this call always
  955.         succeeds.
  956.  
  957.  
  958. THE CLOCK$ DEVICE├─────────────────────────────────────────────────────────────
  959.  
  960.  To allow a clock board to be integrated into the system for TIME and DATE,
  961. the CLOCK$ device is used. This device defines and performs functions like any
  962. other character device (most functions will be reset done bit, reset error bit,
  963. and return). When a read or write to this device occurs, 6 bytes are
  964. transferred. The first 2 bytes are a word, which is the count of days since
  965. 01-01-80. The third byte is minutes, the fourth is hours, the fifth is
  966. hundredths of a second, and the sixth is seconds.
  967.  
  968.  Reading the CLOCK$ device gets the date and time, writing to it sets the date
  969. and time. CLOCK$ is normally called only when the system is initializing or if
  970. the system time and date are set (DOS 3.3+). DOS carries the system time and
  971. date internally after receiving it from the CLOCK$ driver.
  972.  
  973.  
  974.  
  975.  
  976.